<template>
  <el-container>
    <el-header class="menu-header">
      <el-menu mode="horizontal" router :ellipsis="false" default-active="/home">
        <el-menu-item index="/home">
          <template #title>
            <HomeFilled style="width: 1em; height: 1em; margin-right: 8px" />
            主页
          </template>
        </el-menu-item>
        <el-menu-item index="/todo">
          <template #title>
            <List style="width: 1em; height: 1em; margin-right: 8px" />
            待办事项
          </template>
        </el-menu-item>
        <el-sub-menu index="/user">
          <template #title>
            <UserFilled style="width: 1em; height: 1em; margin-right: 8px" />
            {{ username }}
          </template>
          <el-menu-item index="/login" @click="handleLogout">
            <template #title>
              <SwitchButton style="width: 1em; height: 1em; margin-right: 8px" />
              退出登录
            </template>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-header>
    <el-main class="router-view-container">
      <router-view></router-view>
    </el-main>
    <el-footer>
      <CopyRight />
    </el-footer>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useUserStore } from '@/stores/user';
import { useRouter } from 'vue-router';
import CopyRight from '@/views/copyright/index.vue'

const userStore = useUserStore()
const router = useRouter()

const username = computed(() => userStore.username)

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<style>
.el-menu--horizontal>.el-menu-item:nth-last-child(2) {
  margin-right: auto;
}

.router-view-container {
  min-height: 88vh;
}

.menu-header {
  position: sticky;
  top: 0;
  z-index: 1000;
}
</style>
